#include <assert.h>
#include "storeconfig.h"


static shooter_ptr g_pShooter = NULL;
extern guiimport_t g_gui_imp;

shooter_ptr GetShooterPtr(void)
{
	if (NULL == g_pShooter)
	{
		g_pShooter = (shooter_ptr)Call_(g_gui_imp.zm, sizeof(shooter_t));
		g_pShooter->pDate = (date_ptr)Call_(g_gui_imp.zm, sizeof(date_t));
	}

	return g_pShooter;
}

void DropShooterPtr(void)
{
	if (NULL != g_pShooter)
	{
		Call_(g_gui_imp.zf, g_pShooter->pDate);
		Call_(g_gui_imp.zf, g_pShooter);
		g_pShooter = NULL;
	}
}

/* assume that incoming pointer is non-null and local pointer of 
	global object is also non-null, or report an error */
void GetConfig(shooter_ptr pShooter)
{
	shooter_ptr pShooter2 = NULL;
	assert(NULL != pShooter);
	pShooter2 = GetShooterPtr();
	strcpy(pShooter->name, pShooter2->name);
	memcpy(pShooter->pDate, pShooter2->pDate, sizeof(date_t));
	pShooter->trainMode = pShooter2->trainMode;
	pShooter->techType = pShooter2->techType;
}

void SetShooter(char* pName, date_ptr pDate, TRAINMODEENUM mode)
{
	g_pShooter = GetShooterPtr();
	strcpy(g_pShooter->name, pName);
	memcpy(g_pShooter->pDate, pDate, sizeof(date_t));
	g_pShooter->trainMode = mode;

	g_gui_imp.Sys_Printf("name:\t%s\nyear:\t%d\nmonth:\t%d\nday:\t%d\nmode:\t%d\n",
						g_pShooter->name, g_pShooter->pDate->year,
						g_pShooter->pDate->month, g_pShooter->pDate->day,
						g_pShooter->trainMode);
}

void SetShooter2(TECHNIQUETYPE type, double dist, unsigned int rate)
{
	g_pShooter = GetShooterPtr();
	g_pShooter->techType = type;
	g_pShooter->dInitSTDist = dist;
	g_pShooter->nMagRate = rate;

	g_gui_imp.Sys_Printf("type:\t%d\ndist:\t%f\nrate:\t%d\n",
						g_pShooter->techType, g_pShooter->dInitSTDist,
						g_pShooter->nMagRate);
}
